import request from '@/http';
import { ApiResponse } from '@/types/account';
import { WebsiteConfig } from '@/types/website';

// 图片项接口
export interface ImageItem {
  url: string;
  filename: string;
  upload_time: string;
}

enum Url {
  websiteConfig = '/api/website/',
  uploadImage = '/api/images/',
}

export const websiteService = {
  // 获取网站配置
  async getWebsiteConfig(): Promise<ApiResponse<WebsiteConfig>> {
    const response = await request.get(Url.websiteConfig);
    return response.data;
  },

  // 更新网站配置
  async updateWebsiteConfig(config: WebsiteConfig): Promise<ApiResponse<null>> {
    const response = await request.post(Url.websiteConfig, config);
    return response.data;
  },

  // 上传图片
  async uploadImage(file: File): Promise<string> {
    const formData = new FormData();
    formData.append('image', file);
    
    const response = await request.post(Url.uploadImage, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
    
    if (response.data?.code === 200 && response.data?.data?.url) {
      // 返回完整图片URL
      return request.getBaseURL() + response.data.data.url;
    } else {
      throw new Error('上传失败');
    }
  },

  // 获取图片列表
  async getImages(): Promise<ImageItem[]> {
    const response = await request.get(Url.uploadImage);
    
    if (response.data?.code === 200 && Array.isArray(response.data?.data)) {
      // 为每个图片URL添加baseURL
      return response.data.data.map((image: ImageItem) => ({
        ...image,
        url: request.getBaseURL() + image.url
      }));
    } else {
      throw new Error('获取图片列表失败');
    }
  },
}; 